{Name: Lu Wang                                                              }
{Date: March 4, 1996                                                        }
{Purpose: The purpose of this program is to show the process involved when  }
{         Carbon is bonded to four Hydrogen atoms.                          }

PROGRAM Program6;

USES CRT, GRAPH;

{This program also uses CREDITS.TXT file to display the credits}
CONST

     Wait = 50;                       {Delay constants}
     Wait1 = 5;
     Wait2 = 1500;
     Wait3 = 100;

     Radius_s = 60;                    {Radius of electrons orbitals}

     Choice : ARRAY [1..5] OF STRING   {menu choices}
                   = ('Hybrid orbitals', 'Simulation', 'Copyright',
                      'Credits', 'Exit');

{============================Functions======================================}
FUNCTION H_Orbit (x : REAL) : REAL;

{This function calculates the root of the function:
 2 (x ^ 2 + y ^ 2) ^ 2 = 25 (x ^ 2 - y ^ 2), the graph is a lemniscate.}

VAR
     z, temp : REAL;         {holds the results of the intermediate steps}

BEGIN

     temp := SQRT(SQR((4 * SQR(x) + 25)) + 8 * (25 * SQR(x) - 2 * SQR(SQR(x))));
     z := ((-4 * SQR(x) - 25) + temp) / 4;
     H_Orbit := SQRT (z);

END; {end of function}

FUNCTION S_Orbit (x : REAL) : INTEGER;

{This function calculates the root of the function:
 x ^ 2 + y ^ 2 = r ^ 2, the graph is a circle.}

BEGIN

     S_Orbit := ROUND (SQRT (SQR(Radius_s) - SQR(x)));

END; {end of function}

FUNCTION Angle (c : INTEGER; Increment : REAL) : REAL;

BEGIN
     Angle := (3 - c) * 120 * PI / 180 + Increment;

END; {end of function}

{Function X_Rotate and Y_Rotate rotates the coordinate (x,y) by Ang}

FUNCTION X_Rotate (x, y : INTEGER; Ang : REAL) : INTEGER;

BEGIN
     X_Rotate := ROUND (x * COS (Ang) + y * SIN (Ang));

END; {end of function}

FUNCTION Y_Rotate (x, y : INTEGER; Ang : REAL) : INTEGER;

BEGIN
     Y_Rotate := ROUND (-x * SIN (Ang) + y * COS (Ang));

END; {end of function}

{============================Procedures=====================================}
PROCEDURE SetUp;

VAR
     gd,                               {Graphics Driver}
     gm,                               {Graphics Mode}
     err : INTEGER;                    {Error Code}

BEGIN
     gd := DETECT;
     INITGRAPH (gd, gd, ' ');

     err := GraphResult;
     IF err <> grOK
          THEN BEGIN
                    WRITELN ('Graphics error = ', GraphErrorMsg (err));
                    READLN;
                    HALT;
               END; {If}

END; {end of procedure}

PROCEDURE Star_Field (MaxX, MaxY, MinX, MinY : INTEGER);

VAR
     Count, Count1 : INTEGER;          {loop variables}

     x, y : ARRAY [1..400] OF INTEGER; {x, y coordinates of the stars}

BEGIN

     Count := 1;

     FOR Count := 1 TO 400 DO
          BEGIN
               x [Count] := RANDOM (MaxX) + MinX;
               y [Count] := RANDOM (MaxY) + MinY;
          END; {for}

     Count := 1;

     WHILE Count <= 400 DO
          BEGIN

               PUTPIXEL (x [Count], y [Count], WHITE);
               PUTPIXEL (x [Count + 1], y [Count + 1], LIGHTGRAY);
               PUTPIXEL (x [Count + 2], y [Count + 2], DARKGRAY);
               PUTPIXEL (x [Count + 3], y [Count + 3], LIGHTRED);
               PUTPIXEL (x [Count + 4], y [Count + 4], YELLOW);
               Count := Count + 5;
          END; {while}

END; {end of procedure}

PROCEDURE Draw_Underline (Center_X : INTEGER);

CONST
     Wait = 50;                        {delay constant}

VAR
     x : INTEGER;                      {loop variable}

BEGIN

     x := 0;

     WHILE x < 140 DO
          BEGIN
               SETFILLSTYLE (SOLIDFILL, RED);
               SETPALETTE (RED, 100);
               BAR (Center_X - x, 230, Center_X + x, 240);

               SETFILLSTYLE (SOLIDFILL, BLUE);
               BAR (Center_X - x, 280, Center_X + x, 290);
               DELAY (Wait);

               x := x + 3;
          END; {for}

END; {end of procedure}

PROCEDURE Header;

CONST

     Start_Y1 = 200;                   {y coordinate of the header}
     Start_Y2 = 250;

VAR
     Center_X,                         {center of the screen}
     x1, x2,                           {x coordinate of the text}
     y : INTEGER;                      {y coordinate of the credits}

     Credit, Name : STRING;            {text from the CREDITS.TXT}

     File1 : TEXT;                     {file handle}

BEGIN

     SETBKCOLOR (BLACK);

     Center_X := GETMAXX DIV 2;
     x1 := 1;
     x2 := 614;
     SETTEXTSTYLE (SMALLFONT, HORIZDIR, 8);
     SETFILLSTYLE (SOLIDFILL, BLACK);

     {Move text across the screen}
     WHILE x1 < 182 DO
          BEGIN

               SETCOLOR (LIGHTGREEN);
               OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
               OUTTEXTXY (x2, Start_Y2, 'Program #6');

               SETCOLOR (BLACK);
               OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
               OUTTEXTXY (x2, Start_Y2, 'Program #6');

               x1 := x1 + 2;
               x2 := x2 - 4;

          END; {while}

          SETCOLOR (WHITE);
          OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
          OUTTEXTXY (x2, Start_Y2, 'Program #6');

          Draw_Underline (Center_X);

          Star_Field (640, 480, 1, 1);

          DELAY (Wait2);
          SETCOLOR (LIGHTGRAY);
          OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
          OUTTEXTXY (x2, Start_Y2, 'Program #6');

          {GRADUAL FADE OF THE LINE}
          DELAY (Wait2);
          SETCOLOR (DARKGRAY);
          OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
          OUTTEXTXY (x2, Start_Y2, 'Program #6');
          SETPALETTE (RED, RED);
          SETPALETTE (LIGHTBLUE, BLUE);

          DELAY (Wait2);
          SETCOLOR (BLACK);
          SETFILLSTYLE (SOLIDFILL, BLACK);
          OUTTEXTXY (x1, Start_Y1, 'Computer Science 12');
          FLOODFILL (Center_X, 235, BLACK);
          OUTTEXTXY (x2, Start_Y2, 'Program #6');
          FLOODFILL (Center_X, 285, BLACK);

END; {end of procedure}

PROCEDURE Credits;

CONST
     Center_X = 377;

VAR
     y : INTEGER;                      {loop variable}
     Credit, Name : STRING;            {text from CREDITS.TXT}
     File1 : TEXT;                     {file handle for CREDITS.TXT}
     ch : CHAR;

BEGIN

     {Display credits}
     ASSIGN (File1, 'CREDITS.TXT');
     RESET (File1);

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (377, 280, LIGHTBLUE);

     SETCOLOR (WHITE);
     OUTTEXTXY (184, 444, 'Press any key to return to menu');

     WHILE (NOT EOF (File1)) AND (NOT KEYPRESSED) DO
          BEGIN

               READLN (File1, Credit);
               READLN (File1, Name);

               SETTEXTJUSTIFY (CENTERTEXT, TOPTEXT);
               y := 370;

               Star_Field (360, 280, 190, 140);

               {Move the text from the bottom of the screen up to the
                middle of the screen}
               WHILE y >= 215 DO
                    BEGIN

                         SETCOLOR (WHITE);
                         OUTTEXTXY (Center_X, y, Credit);
                         OUTTEXTXY (Center_X, y + 52, Name);

                         DELAY (Wait3);

                         SETCOLOR (BLACK);
                         OUTTEXTXY (Center_X, y, Credit);
                         OUTTEXTXY (Center_X, y + 52, Name);
                         y := y - 2;

                    END; {while}

               SETCOLOR (WHITE);
               OUTTEXTXY (Center_X, y, Credit);
               SETCOLOR (YELLOW);
               OUTTEXTXY (Center_X, y + 52, Name);

               {Underline the text}
               Draw_Underline (Center_X);
               DELAY (Wait2);

               SETFILLSTYLE (SOLIDFILL, BLACK);
               FLOODFILL (Center_X, 235, BLACK);
               FLOODFILL (Center_X, 285, BLACK);

               {Move the text to the top of the screen}
               WHILE y >= 135 DO
                    BEGIN
                         IF y > 160
                              THEN SETCOLOR (LIGHTGRAY)
                              ELSE SETCOLOR (DARKGRAY);

                         OUTTEXTXY (Center_X, y, Credit);
                         OUTTEXTXY (Center_X, y + 52, Name);

                         DELAY (Wait3);

                         SETCOLOR (BLACK);
                         OUTTEXTXY (Center_X, y, Credit);
                         OUTTEXTXY (Center_X, y + 52, Name);
                         y := y - 2;
                    END; {while}

               DELAY (Wait2);

               FLOODFILL (Center_X, 280, LIGHTBLUE);

          END; {while}

     CLOSE (File1);

     {Clear keyboard buffer}
     ch := READKEY;

     SETTEXTJUSTIFY (LEFTTEXT, TOPTEXT);
     SETCOLOR (LIGHTBLUE);
     OUTTEXTXY (184, 444, 'Press any key to return to menu');

END; {end of procedure}

PROCEDURE Write_Text (x, y : INTEGER;
                      s : STRING;
                      Color, Color1 : WORD);

VAR
     c : INTEGER;                      {loop variable}

BEGIN

     SETCOLOR (WHITE);
     FOR c := 1 TO 6 DO
          OUTTEXTXY (x + c, y + c, s);

     SETCOLOR (Color);
     OUTTEXTXY (x + c, y + c, s);

     DELAY (Wait);
     SETPALETTE (Color, Color1);

     DELAY (Wait);
     SETPALETTE (Color, Color1 + 1);

END; {end of procedure}

PROCEDURE Texts;

{This procedure display the purpose of the program}
VAR
     c : INTEGER;                      {loop variable}
     s : STRING;                       {one line of text from INTRO.TXT}
     ch : CHAR;
     File1 : TEXT;                     {file handle for INTRO.TXT}

BEGIN

     {Display the name, due date, and purpose}
     Star_Field (640, 480, 1, 1);

     SETTEXTJUSTIFY (LEFTTEXT, CENTERTEXT);
     SETTEXTSTYLE (TRIPLEXFONT, HORIZDIR, 2);

     Write_Text (1, 10, 'Name: ', 2, 9);
     Write_Text (1, 50, 'Due Date: ', 2, 9);
     Write_Text (1, 90, 'Purpose: ', 2, 9);

     ASSIGN (File1, 'INTRO.TXT');
     RESET (File1);

     READLN (File1, s);
     Write_Text (100, 10, s, 5, 13);
     READLN (File1, s);
     Write_Text (100, 50, s, 4, 12);

     FOR c := 0 TO 7 DO
          BEGIN
               READLN (File1, s);
               Write_Text (100, 90 + c * 50, s, 3, 11);
          END; {for}

     {Inform the user to press a key}
     SETTEXTJUSTIFY (CENTERTEXT, TOPTEXT);
     Write_Text (320, 430, 'PRESS ANY KEY TO CONTINUE', 1, 8);

     CLOSE (File1);
     ch := READKEY;

END; {end of procedure}

PROCEDURE View_Screen;

{This procedure draws the view screen}

BEGIN

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (1, 1, BROWN);
     SETCOLOR (LIGHTBLUE);

     ARC (580, 50, 350, 100, 50);      {Arc1-A}
     ARC (50, 40, 90, 180, 40);        {Arc2-A}
     LINE (580, 0, 50, 0);             {Line, Arc1-A to Arc2-A}

     ARC (50, 430, 180, 270, 30);      {Arc3-A}
     LINE (10, 40, 20, 430);           {Line, Arc2-A to Arc3-A}

     ARC (580, 410, 270, 0, 50);       {Arc4-A}
     LINE (50, 460, 580, 460);         {Line, Arc3-A to Acr4-A}
     LINE (630, 410, 630, 50);         {Line, Arc4-A to Acr1-A}

     ARC (520, 70, 350, 100, 50);      {Arc1-B}
     ARC (520, 390, 270, 0, 50);       {Arc4-B}
     LINE (570, 390, 570, 70);         {Line, Arc1-B to Arc4-B}

     ARC (50, 40, 90, 180, 20);        {Arc2-B}
     LINE (520, 20, 50, 20);           {Line, Arc1-B to Arc2-B}

     ARC (50, 430, 180, 270, 10);      {Arc3-B}
     LINE (30, 40, 40, 430);           {Line, Arc3-B to Arc2-B}
     LINE (50, 440, 520, 440);         {Line, Arc3-B to Arc4-B}

     SETFILLSTYLE (SOLIDFILL, LIGHTBLUE);
     FLOODFILL (28, 40, LIGHTBLUE);

     BAR (32, 100, 570, 120);
     BAR (160, 120, 180, 440);

     SETTEXTJUSTIFY (LEFTTEXT, TOPTEXT);
     SETTEXTSTYLE (DEFAULTFONT, HORIZDIR, 1);
     SETCOLOR (WHITE);
     OUTTEXTXY (50, 36, 'Hybridization of Carbon atom''s electrons in CH4');

END; {end of procedure}

PROCEDURE Hybrid;

{This procedure prints out information on hybrid orbitals}

VAR
     c : INTEGER;                      {loop variable}
     s : STRING;                       {one line of text from HYBRID.TXT}
     File1 : TEXT;                     {file handle}

BEGIN

     ASSIGN (File1, 'HYBRID.TXT');
     RESET (File1);

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (377, 280, LIGHTBLUE);

     SETCOLOR (LIGHTCYAN);
     SETTEXTSTYLE (DEFAULTFONT, HORIZDIR, 1);

     FOR c := 1 TO 16 DO
          BEGIN
               READLN (File1, s);
               OUTTEXTXY (188, 120 + c * 12, s);
          END;

     CLOSE (File1);
     SETTEXTSTYLE (SMALLFONT, HORIZDIR, 5);

END; {end of procedure}

PROCEDURE Carbon;

{This procedure draws the nucleus of the carbon atom}
BEGIN

     SETCOLOR (BLUE);
     SETFILLSTYLE (SOLIDFILL, BLUE);

     CIRCLE (345, 270, 10);
     FLOODFILL (345, 270, BLUE);

     CIRCLE (390, 270, 10);
     FLOODFILL (390, 270, BLUE);

     CIRCLE (350, 300, 10);
     FLOODFILL (350, 300,BLUE);

     CIRCLE (370, 300, 10);
     FLOODFILL (370, 300, BLUE);

     CIRCLE (390, 310, 10);
     FLOODFILL (390, 310, BLUE);

     CIRCLE (365, 270, 10);
     FLOODFILL (365, 270, BLUE);

     SETCOLOR (RED);
     SETFILLSTYLE (SOLIDFILL, RED);

     CIRCLE (360, 290, 10);
     FLOODFILL (360,290,RED);

     CIRCLE (340, 290, 10);
     FLOODFILL (340, 290, RED);

     CIRCLE (380, 275, 10);
     FLOODFILL (380, 275, RED);

     CIRCLE (380, 295, 10);
     FLOODFILL (380, 295, RED);

     CIRCLE (400, 280, 10);
     FLOODFILL (400, 280, RED);

     CIRCLE (390, 300, 10);
     FLOODFILL (390, 300, RED);

END; {end of procedure}

PROCEDURE Hybridize (p, q : POINTER);

VAR
     x, y,
     x2, y2,                           {coordinates of the Carbon atom's}
     x3, y3,                           {electrons}
     x4, y4,
     x5, y5,

     Hx, Hy,                           {coordinates of the Hydrogen atoms}
     Ex, Ey,                           {coordinates of the Hydrogen atoms'}
                                       {electrons}

     c, d, e : INTEGER;                {loop variables}

     x1,                               {loop variables}
     Ang : REAL;                       {angle of rotation}

     CH : CHAR;

     Ex2, Ey2,                           {final coordinates of Hydrogen atoms}
     Hx2, Hy2 : ARRAY [1..4] OF INTEGER; {and their electrons}

BEGIN

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (377, 200, LIGHTBLUE);

     SETCOLOR (WHITE);
     OUTTEXTXY (50, 444, 'C atom''s electrons form sp3 hybrid');
     OUTTEXTXY (314, 444, 'orbits and bond with four H atoms');

     e := 1;
     WHILE (e <= 6) AND (NOT KEYPRESSED) DO
          BEGIN

               FOR c := 0 TO 2 DO
                    BEGIN

                         Ang := Angle (c, 0);
                         Ex := ROUND (COS (ANG) * 120) + 370;
                         Ey := ROUND (SIN (ANG) * 30) + 380;
                         Hx2 [c+1] := Ex-10;
                         Hy2 [c+1] := Ey-10;

                         PUTIMAGE (Hx2 [c+1], Hy2 [c+1], q^, NORMALPUT);

                    END; {for}

               PUTIMAGE (358, 138, q^, NORMALPUT);

               Carbon;
               SETPALETTE (RED, RED);

               {Show the electrons in sp3 orbitals around the Carbon atom}
               x1 := -3.4;

               WHILE x1 < 3.4 DO
                    BEGIN
                         x1 := x1 + 0.1;

                         IF x1 < 0
                              THEN BEGIN
                                        y := ROUND (6 * H_Orbit (x1)) * -1;
                                        x := ROUND (6 * x1);
                                   END {if then}

                              ELSE BEGIN
                                        y := ROUND (12 * H_Orbit (x1)) * -1;
                                        x := ROUND (18 * x1);
                                   END; {if else}


                         x2 := X_Rotate (x, y, 8 * PI / 3) * -1;
                         y2 := Y_Rotate (x, y, 8 * PI / 3) * -1;
                         x3 := X_Rotate (x, y, PI / 2);
                         y3 := Y_Rotate (x, y, PI / 2);
                         x4 := ROUND (X_Rotate (x, y, 7 * PI / 5) / 1.5);
                         y4 := ROUND (Y_Rotate (x, y, 7 * PI / 5) / 1.5);
                         x5 := ROUND (X_Rotate (x, y, 4 * PI / 3) / 4);
                         y5 := ROUND (Y_Rotate (x, y, 4 * PI / 3) / 4);

                         {Show the path of sp3 orbitals}
                         PUTPIXEL (x2 + 435, y2 + 305, WHITE);
                         PUTPIXEL (x3 + 367, y3 + 230, WHITE);
                         PUTPIXEL (x4 + 340, y4 + 335, WHITE);
                         PUTPIXEL (x5 + 330, y5 + 310, WHITE);

                         {Show the electrons}
                         PUTIMAGE (x2 + 430, y2 + 300, p^, XORPUT);
                         PUTIMAGE (x3 + 362, y3 + 225, p^, XORPUT);
                         PUTIMAGE (x4 + 335, y4 + 330, p^, XORPUT);
                         PUTIMAGE (x5 + 325, y5 + 305, p^, XORPUT);

                         DELAY (Wait);

                         {Erase the electrons}
                         PUTIMAGE (x2 + 430, y2 + 300, p^, XORPUT);
                         PUTIMAGE (x3 + 362, y3 + 225, p^, XORPUT);
                         PUTIMAGE (x4 + 335, y4 + 330, p^, XORPUT);
                         PUTIMAGE (x5 + 325, y5 + 305, p^, XORPUT);

                    END; {while x1 < 3.4}

               x1 := 3.4;

               WHILE x1 > -3.4 DO
                    BEGIN
                         x1 := x1 - 0.1;

                         IF x1 < 0
                              THEN BEGIN
                                        y := ROUND (6 * H_Orbit (x1));
                                        x := ROUND (6 * x1);
                                   END {if then}

                              ELSE BEGIN
                                        y := ROUND (12 * H_Orbit (x1));
                                        x := ROUND (18 * x1);
                                   END; {if else}


                         x2 := X_Rotate (x, y, 8 * PI / 3) * -1;
                         y2 := Y_Rotate (x, y, 8 * PI / 3) * -1;
                         x3 := X_Rotate (x, y, PI / 2);
                         y3 := Y_Rotate (x, y, PI / 2);
                         x4 := ROUND (X_Rotate (x, y, 7 * PI / 5) / 1.5);
                         y4 := ROUND (Y_Rotate (x, y, 7 * PI / 5) / 1.5);
                         x5 := ROUND (X_Rotate (x, y, 4 * PI / 3) / 4);
                         y5 := ROUND (Y_Rotate (x, y, 4 * PI / 3) / 4);

                         {Shows the path of sp3 orbitals}
                         PUTPIXEL (x2 + 435, y2 + 305, WHITE);
                         PUTPIXEL (X3 + 367, Y3 + 230, WHITE);
                         PUTPIXEL (x4 + 340, y4 + 335, WHITE);
                         PUTPIXEL (x5 + 330, y5 + 310, WHITE);

                         {Shows the electrons}
                         PUTIMAGE (X2 + 430, Y2 + 300, p^, XORPUT);
                         PUTIMAGE (x3 + 362, y3 + 225, p^, XORPUT);
                         PUTIMAGE (x4 + 335, y4 + 330, p^, XORPUT);
                         PUTIMAGE (x5 + 325, y5 + 305, p^, XORPUT);

                         DELAY (Wait);

                         {Erase the electrons}
                         PUTIMAGE (x2 + 430, y2 + 300, p^, XORPUT);
                         PUTIMAGE (x3 + 362, y3 + 225, p^, XORPUT);
                         PUTIMAGE (x4 + 335, y4 + 330, p^, XORPUT);
                         PUTIMAGE (x5 + 325, y5 + 305, p^, XORPUT);

                    END; {while x1 >-3.4}

               {Show Hydrogen's electron orbiting the nucleus}
               FOR c := -Radius_S TO Radius_S DO
                    BEGIN
                         FOR d := 1 TO 3 DO
                              BEGIN
                                   Ex2 [d] := c DIV 2 + Hx2 [d] + 5;
                                   Ey2 [d] := (S_Orbit (c)) DIV 2 + Hy2 [d] + 5;

                              END; {for d}

                         Ex2 [4] := c DIV 2 + 363;
                         Ey2 [4] := (S_Orbit (c)) DIV 2 + 143;

                         FOR d := 1 TO 4 DO
                              BEGIN
                                   PUTIMAGE (Ex2 [d], Ey2 [d], p^, XORPUT);
                                   DELAY (Wait1);
                                   PUTIMAGE (Ex2 [d], Ey2 [d], p^, XORPUT);

                              END; {for d}

                    END; {for c}

               FOR c := Radius_S DOWNTO -Radius_S DO
                    BEGIN
                         FOR d := 1 TO 3 DO
                              BEGIN
                                   Ex2 [d] := c DIV 2 + Hx2 [d] + 5;
                                   Ey2 [d] := (S_Orbit (c)) DIV -2 + Hy2 [d] + 5;

                              END; {for d}

                              Ex2 [4] := c DIV 2 + 363;
                              Ey2 [4] := (S_Orbit (c)) DIV -2 + 143;

                         FOR d := 1 TO 4 DO
                              BEGIN
                                   PUTIMAGE (Ex2 [d], Ey2 [d], p^, XORPUT);
                                   DELAY (Wait1);
                                   PUTIMAGE (Ex2 [d], Ey2 [d], p^, XORPUT);

                              END; {for d}

                    END; {for c}

               DELAY (Wait2);
               SETFILLSTYLE (SOLIDFILL, BLACK);
               FLOODFILL (400, 400, LIGHTBLUE);

               e := e + 1;
          END; {while e <= 6}

     {Clear keyboard buffer}
     ch := READKEY;


     SETCOLOR (LIGHTBLUE);
     OUTTEXTXY (50, 444, 'C atom''s electrons form sp3 hybrid');
     OUTTEXTXY (314, 444, 'orbits and bond with four H atoms');
     OUTTEXTXY (184, 104, 'Press ESC key to skip to the next part');

END; {end of procedure}

PROCEDURE Rotate (p, q : POINTER);

VAR

     Hx, Hy,                             {coordinates of the Hydrogen atoms}
     Ex, Ey,                             {coordinates of the electrons}

     c, d,                               {loop variables}
     count : INTEGER;

     Ang,                                {angle of rotation}
     Increment : REAL;                   {increment of angle of rotation}

     s : STRING;                         {one line of text from INFO.DAT}

     Ex2, Ey2,                           {rotational coordinates of the
                                          electrons}

     Hx3, Hy3 : ARRAY [1..4] OF INTEGER; {rotational coordinates of the
                                          Hydrogen atoms}

     File1 : TEXT;                       {File handle of INFO.DAT}

     ch : CHAR;

BEGIN

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (377, 200, LIGHTBLUE);
     FLOODFILL (44, 200, LIGHTBLUE);

     {Print out data about CH4}
     ASSIGN (File1, 'INFO.DAT');
     RESET (File1);

     SETCOLOR (GREEN);
     SETTEXTSTYLE (DEFAULTFONT, HORIZDIR, 1);

     c := 1;
     WHILE NOT EOF (File1) DO
          BEGIN
               READLN (File1, s);
               OUTTEXTXY (44, 115 + c * 15, s);
               c := c + 1;
          END; {while}

     CLOSE (File1);

     SETTEXTSTYLE (SMALLFONT, HORIZDIR, 5);
     SETCOLOR (WHITE);
     OUTTEXTXY (184, 104, 'Press any key to return to menu');

     count := 0;
     Increment := 0;

     WHILE NOT KEYPRESSED DO
          BEGIN

               IF ROUND (Increment * 1000) = ROUND (2000 * PI)
                    THEN BEGIN
                              Increment := 0;
                              Count := 0;
                         END {if then}

                    ELSE BEGIN
                              Increment := Count * PI / 100;
                              Count := Count + 1;
                         END; {if then}


               FOR c := 0 TO 2 DO
                    BEGIN

                         Ang := Angle (c, Increment);

                         Hx := ROUND (COS (ANG) * 40) + 370;
                         Hy := ROUND (SIN (ANG) * 10) + 280;

                         Ex := ROUND (COS (ANG) * 120) + 370;
                         Ey := ROUND (SIN (ANG) * 30) + 380;

                         Hx3 [c+1] := Ex-10;
                         Hy3 [c+1] := Ey-10;

                         PUTIMAGE (Hx3 [c+1], Hy3 [c+1], q^, NORMALPUT);

                         SETCOLOR (WHITE);
                         LINE (Hx, Hy, Ex, Ey);

                    END; {for}

               PUTIMAGE (358, 138, q^, NORMALPUT);
               LINE (368, 148, 368, 260);

               Carbon;
               SETPALETTE (RED, RED);

               DELAY (Wait2);
               SETFILLSTYLE (SOLIDFILL, BLACK);
               FLOODFILL (400, 300, LIGHTBLUE);

          END; {while not keypressed}

     {Clear the keyboard buffer}
     ch := READKEY;

     SETCOLOR (LIGHTBLUE);
     OUTTEXTXY (184, 104, 'Press any key to return to menu');

     {Reprint the menu}
     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (44, 200, LIGHTBLUE);

     SETCOLOR (WHITE);

     FOR c := 1 TO 5 DO
          OUTTEXTXY (44, 115 + c * 15, Choice [c]);

     SETCOLOR (YELLOW);
     OUTTEXTXY (44, 145, Choice [2]);

END; {end of procedure}

PROCEDURE Bonding;

{This procedure shows the process involved when Carbon is bonded to three
 Hydrogen atoms}
VAR
     x, y,                             {coordinates of the Carbon atom's}
     x1, y1,                           {electron}
     x2, y2,
     x3, y3,

     Hx1, Hy1,                         {coordinates of the Hydrogen atoms}
     Hx2, Hy2,
     Hx3, Hy3,
     Hx4, Hy4 : INTEGER;

     c : REAL;                         {temporary storage variable}

     p, q : POINTER;                   {memory location of electron and}
                                       {Hydrogen atom images}

     size, size1 : WORD;               {size of electron and Hydrogen atom}
                                       {images}

     ch : CHAR;

BEGIN

     SETACTIVEPAGE (1);

     {draws an electron}
     SETCOLOR (LIGHTGRAY);
     CIRCLE (20, 20, 5);
     SETFILLSTYLE (SOLIDFILL, LIGHTGRAY);
     FLOODFILL (20, 20, LIGHTGRAY);

     size := IMAGESIZE (15, 15, 25, 25);
     GETMEM (p, size);
     GETIMAGE (15, 15, 25, 25, p^);

     {draws a Hydrogen}
     SETCOLOR (RED);
     CIRCLE (60, 60, 10);
     SETFILLSTYLE (SOLIDFILL, RED);
     FLOODFILL (60, 60, RED);

     size1 := IMAGESIZE (50, 50, 70, 70);
     GETMEM (q, size1);
     GETIMAGE (50, 50, 70, 70, q^);

     SETACTIVEPAGE (0);
     SETVISUALPAGE (0);

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (400, 300, LIGHTBLUE);

     {Draw the Carbon atom nucleus}
     Carbon;
     SETPALETTE (RED, RED);

     SETCOLOR (WHITE);
     OUTTEXTXY (100, 444, 'Four Hydrogen atoms are approaching the Carbon atom');
     OUTTEXTXY (184, 104, 'Press ESC key to skip to the next part');

     {Initialize the variables}
     c := 1;

     Hx1 := 220;
     Hy1 := 145;
     Hx2 := 220;
     Hy2 := 395;
     Hx3 := 500;
     Hy3 := 145;
     Hx4 := 500;
     Hy4 := 395;

     WHILE (Hx1 < 240) AND (NOT KEYPRESSED) DO
          BEGIN

               Hx1 := Hx1 + 4;
               Hy1 := Hy1 + 2;
               Hx2 := Hx2 + 4;
               Hy2 := Hy2 - 2;
               Hx3 := Hx3 - 4;
               Hy3 := Hy3 + 2;
               Hx4 := Hx4 - 4;
               Hy4 := Hy4 - 2;

               {Put the image of the Hydrogens}
               PUTIMAGE (Hx1, Hy1, q^, XORPUT);
               PUTIMAGE (Hx2, Hy2, q^, XORPUT);
               PUTIMAGE (Hx3, Hy3, q^, XORPUT);
               PUTIMAGE (Hx4, Hy4, q^, XORPUT);

               x3 := 0;

               {Show Carbon's electrons orbiting the nucleus}
               FOR x := -Radius_s TO Radius_s DO
                    BEGIN

                         y := S_Orbit (x);
                         x1 := ROUND (1.5 * x);
                         y1 := ROUND (1.5 * y);
                         x2 := x DIV 2;
                         y2 := y DIV 2;

                         IF x MOD 4 = 0
                              THEN BEGIN
                                   IF x3 >= 30
                                        THEN x3 := 1
                                        ELSE x3 := x3 + 1;

                                        c := SQR (x3) * x3;
                                        y3 := ROUND (SQRT (c * (30 - x3)) / 3);

                                   END; {if then}

                         {Put the path of Carbon's electrons' orbits}
                         PUTPIXEL (x + 377, y + 285, WHITE);
                         PUTPIXEL (-x + 377, -y + 285, WHITE);
                         PUTPIXEL (3 * x3 + 407, y3 + 285, LIGHTGRAY);
                         PUTPIXEL (y3 + 377, 3 * x3 + 315, DARKGRAY);

                         {Put the image of the four electrons}
                         PUTIMAGE (x + 372, y + 280, p^, XORPUT);
                         PUTIMAGE (-x + 372, -y + 280, p^, XORPUT);
                         PUTIMAGE (3 * x3 + 402, y3 + 280, p^, XORPUT);
                         PUTIMAGE (y3 + 372, 3 * x3 + 310, p^, XORPUT);

                         {Put the image of the Hydrogens' electrons}
                         PUTIMAGE (x2 + Hx1 + 5, y2 + Hy1 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx2 + 5, y2 + Hy2 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx3 + 5, y2 + Hy3 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx4 + 5, y2 + Hy4 + 5, p^, XORPUT);

                         {Erase the image of Carbon's electrons}
                         PUTIMAGE (x + 372, y + 280, p^, XORPUT);
                         PUTIMAGE (-x + 372, -y + 280, p^, XORPUT);
                         PUTIMAGE (3 * x3 + 402, y3 + 280, p^, XORPUT);
                         PUTIMAGE (y3 + 372, 3 * x3 + 310, p^, XORPUT);

                         {Erase the image of Hydrogens' electrons}
                         PUTIMAGE (x2 + Hx1 + 5, y2 + Hy1 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx2 + 5, y2 + Hy2 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx3 + 5, y2 + Hy3 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx4 + 5, y2 + Hy4 + 5, p^, XORPUT);

                    END; {for}

               x3 := 31;

               FOR x := Radius_s DOWNTO -Radius_s DO
                    BEGIN
                         y := S_Orbit (x) * -1;
                         x1 := ROUND (1.5 * x);
                         y1 := ROUND (1.5 * y);
                         x2 := x DIV 2;
                         y2 := y DIV 2;

                         IF x MOD 4 = 0
                              THEN BEGIN
                                   IF x3 <= 0
                                        THEN x3 := 1
                                        ELSE x3 := x3 - 1;
                                        c := SQR (x3) * x3;

                                        y3 := ROUND(SQRT(c * (30 - x3))/3) * -1;
                                   END; {if then}

                         {Show the path Carbon's electrons' orbits}
                         PUTPIXEL (x + 377, y + 285, WHITE);
                         PUTPIXEL (-x + 377, -y + 285, WHITE);
                         PUTPIXEL (3 * x3 + 407, y3 + 285, LIGHTGRAY);
                         PUTPIXEL (y3 + 377, 3 * x3 + 315, DARKGRAY);

                         {Show the Carbon's electrons}
                         PUTIMAGE (x + 372, y + 280, p^, XORPUT);
                         PUTIMAGE (-x + 372, -y + 280, p^, XORPUT);
                         PUTIMAGE (3 * x3 + 402, y3 + 280, p^, XORPUT);
                         PUTIMAGE (y3 + 372, 3 * x3 + 310, p^, XORPUT);

                         {Show Hydrogen atoms' electrons}
                         PUTIMAGE (x2 + Hx1 + 5, y2 + Hy1 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx2 + 5, y2 + Hy2 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx3 + 5, y2 + Hy3 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx4 + 5, y2 + Hy4 + 5, p^, XORPUT);

                         {Erase the image of Carbons' electrons}
                         PUTIMAGE (x + 372, y + 280, p^, XORPUT);
                         PUTIMAGE (-x + 372, -y + 280, p^, XORPUT);
                         PUTIMAGE (3 * x3 + 402, y3 + 280, p^, XORPUT);
                         PUTIMAGE (y3 + 372, 3 * x3 + 310, p^, XORPUT);

                         {Erase the image of Hydrogen atoms' electrons}
                         PUTIMAGE (x2 + Hx1 + 5, y2 + Hy1 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx2 + 5, y2 + Hy2 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx3 + 5, y2 + Hy3 + 5, p^, XORPUT);
                         PUTIMAGE (x2 + Hx4 + 5, y2 + Hy4 + 5, p^, XORPUT);

                    END; {for}

               {Show the Hydrogen atom}
               PUTIMAGE (Hx1, Hy1, q^, XORPUT);
               PUTIMAGE (Hx2, Hy2, q^, XORPUT);
               PUTIMAGE (Hx3, Hy3, q^, XORPUT);
               PUTIMAGE (Hx4, Hy4, q^, XORPUT);

          END; {while}

     {Show the image of Carbon atom's electrons}
     PUTIMAGE (x + 372, y + 280, p^, XORPUT);
     PUTIMAGE (-x + 372, -y + 280, p^, XORPUT);
     PUTIMAGE (3 * x3 + 402, y3 + 280, p^, XORPUT);
     PUTIMAGE (y3 + 372, 3 * x3 + 310, p^, XORPUT);

     {Show the four Hydrogen atoms}
     PUTIMAGE (Hx1, Hy1, q^, XORPUT);
     PUTIMAGE (Hx2, Hy2, q^, XORPUT);
     PUTIMAGE (Hx3, Hy3, q^, XORPUT);
     PUTIMAGE (Hx4, Hy4, q^, XORPUT);

     {Show Hydrogen atoms' electrons}
     PUTIMAGE (x2 + Hx1 + 5, y2 + Hy1 + 5, p^, XORPUT);
     PUTIMAGE (x2 + Hx2 + 5, y2 + Hy2 + 5, p^, XORPUT);
     PUTIMAGE (x2 + Hx3 + 5, y2 + Hy3 + 5, p^, XORPUT);
     PUTIMAGE (x2 + Hx4 + 5, y2 + Hy4 + 5, p^, XORPUT);

     DELAY (Wait2);

     {Clear keyboard buffer}
     ch := READKEY;

     SETCOLOR (LIGHTBLUE);
     OUTTEXTXY (100, 444, 'Four Hydrogen atoms are approaching the Carbon atom');

     Hybridize (p, q);
     Rotate (p, q);

END; {end of procedure}

PROCEDURE Copyright;

VAR
     c : INTEGER;                      {loop variable}
     s : STRING;                       {one line of text from COPYRIT3.TXT}
     File1 : TEXT;                     {file handle}

BEGIN

     SETFILLSTYLE (SOLIDFILL, BLACK);
     FLOODFILL (400, 250, LIGHTBLUE);

     ASSIGN (File1, 'COPYRIT3.TXT');
     RESET (File1);

     SETCOLOR (WHITE);
     OUTTEXTXY (44, 145, 'Simulation');
     SETCOLOR (YELLOW);
     OUTTEXTXY (44, 160, 'Copyright');

     SETCOLOR (LIGHTCYAN);

     FOR c := 1 TO 16 DO
          BEGIN
               READLN (File1, s);
               OUTTEXTXY (188, 120 + c * 12, s);
          END; {for}

     DELAY (Wait2);

END; {end of procedure}

PROCEDURE Menu;

VAR
     Sel,                              {the menu choice selected by the user}
     c : INTEGER;                      {loop variable}
     ch : CHAR;                        {hold the value of the key pressed}

BEGIN

     SETTEXTSTYLE (SMALLFONT, HORIZDIR, 5);
     SETCOLOR (WHITE);

     {Print out the menu choices}
     FOR c := 1 TO 5 DO
          OUTTEXTXY (44, 115 + c * 15, Choice [c]);


     SETCOLOR (YELLOW);
     OUTTEXTXY (44, 130, Choice [1]);

     Sel := 1;

     WHILE Sel <> 5 DO
          BEGIN

               ch := ' ';

               SETTEXTJUSTIFY (LEFTTEXT, TOPTEXT);
               SETCOLOR (WHITE);
               OUTTEXTXY (50, 444, 'Use up and down arrow key to highlight');
               OUTTEXTXY (350, 444, 'the option and then press enter');

               {Wait for the user to press a key}
               WHILE ch <> CHR (13) DO
                    BEGIN
                         ch := READKEY;

                         {Highlight the selected choice}
                         IF (ch = CHR (72)) OR (ch = CHR (80))
                              THEN BEGIN
                                   SETCOLOR (WHITE);
                                   OUTTEXTXY (44, 115 + Sel * 15, Choice [Sel]);
                                   END;

                         CASE ch OF
                              CHR (72) : BEGIN             {up arrow key}
                                              IF Sel = 1
                                                   THEN Sel := 5
                                                   ELSE Sel := Sel - 1;
                                         END; {case of CHR (72) }

                              CHR (80) : BEGIN             {down arrow key}
                                              IF Sel = 5
                                                   THEN Sel := 1
                                                   ELSE Sel := Sel + 1;
                                         END; {case of CHR (80) }
                         END; {case}

                         SETCOLOR (YELLOW);
                         OUTTEXTXY (44, 115 + Sel * 15, Choice [Sel]);

                    END; {while ch <> CHR (13) }

               SETCOLOR (LIGHTBLUE);
               OUTTEXTXY (50, 444, 'Use up and down arrow key to highlight');
               OUTTEXTXY (350, 444, 'the option and then press enter');

               CASE Sel OF
                    1 : Hybrid;
                    2 : Bonding;
                    3 : Copyright;
                    4 : Credits;

               END; {case}

          END; {while sel <> 5}

END; {end of procedure}

{============================Main Program====================================}
BEGIN
     CLRSCR;
     SetUp;
     Header;
     Texts;
     View_Screen;
     Menu;
     RESTORECRTMODE;
END.

